local game = require "club.threecard.blackjack"

local dealer = game.ui.element()

dealer.store_watchers = 
{
    ingame = 
    {
        ["阶段变更"] = function(self, store, state, args, initialized)
            if state.phase == state.PHASE.IDLE or state.phase == state.PHASE.PREPARE or state.phase == state.PHASE.NULL then
                self:recycle_card();
            end
        end,

        ["游戏开始"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            local banker = args.banker;
            local players = args.players;

            local containers = {};
            local pokers = {};
        
            table.insert(containers, self.banker.poker_container);
            table.insert(pokers, banker);

            local seats = {};
            for _, player in ipairs(players) do
                table.insert(containers, self.seats[player.no].poker_container);
                table.insert(pokers, player.card_ids);
                table.insert(seats, self.seats[player.no]);
            end

            game.coroutine.ingame_start(function()
                for index, container in ipairs(containers) do
                    container.start_pos = self.start_root.position;
                    container:mopai(pokers[index]);

                    game.coroutine.wait(0.5);
                end
            end)
        end,

        ["庄家摸牌"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            self.banker.poker_container.start_pos = self.start_root.position;
            self.banker.poker_container:mopai(state.banker_cards);
        end,

        ["玩家摸牌"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            local no = args.no;
            local card_ids = args.card_ids;
            local hand_index = args.hand_index;
            local seat = self.seats[no]
            if not seat then
                game.gamelib.log.error("错误：没有找到座位 no："..tostring(no));
                return;
            end

            local container = nil;
            if hand_index == 1 then
                container = seat.poker_container2;
            else
                container = seat.poker_container;
            end

            container.start_pos = self.start_root.position;
            container:mopai(card_ids);
        end
    }
}

function dealer:ctor(seats, banker)
    self.res = "club.threecard.blackjack/ui/ingame.b:dealer"
    self.seats = seats;
    self.banker = banker;
end

function dealer:recycle_card()
    local to_recycle_poker = {};

    for _, poker in ipairs(self.banker.poker_container.hand) do
        table.insert(to_recycle_poker, poker);
    end
    for _, seat in ipairs(self.seats) do
        seat.poker_container:hide_amount();
        seat.poker_container2:hide_amount();
        for _, poker in ipairs(seat.poker_container.hand) do
            table.insert(to_recycle_poker, poker);
        end
        for _, poker in ipairs(seat.poker_container2.hand) do
            table.insert(to_recycle_poker, poker);
        end
    end
    
    local anim_time = 0.3;
    local left_count = #to_recycle_poker;
    for _, poker in ipairs(to_recycle_poker) do
        poker.transform:DOMove(self.end_root.position, anim_time)
        :OnComplete(function()
            left_count = left_count - 1;
            if left_count == 0 then
                self.banker.poker_container:clear();
                for _, seat in ipairs(self.seats) do
                    seat.poker_container:clear();
                    seat.poker_container2:clear();
                end
            end
        end)
    end
end

return dealer